/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = stomp_others_parse.c                    */
/*  NOTE      =                                                    */
/*  DATE      = 2015/10/23 by geyh                                 */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "../../../src/module/stomp/stomp_comm.h"
#include "../../../src/module/stomp/stomp_core.h"
#include "../../../src/module/stomp/config.h"
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"
#include "frlist.h"
#include "frreport_comm.h"

#include "apmtest_cunit.h"
#include "apmtest_comm.h"

extern L7session_stomp_private* __stomp_session_private_create(void);

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void __case1(void)
{
    L7session_stomp_private *p_st_private = NULL;
    char *p_data_in = NULL;
    int i_data_len = 0;
    int i_ret = 0;
    char *p_tmp = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = apmtest_create_ct(&p_st_conn, stomp_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 5672);
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    p_st_private = __stomp_session_private_create();

    p_data_in = (char *)frmemdup((char *)"ACK\nreceipt:3\nsubscription:1\n\n\0\n", 32);
    p_tmp = p_data_in;
    i_data_len = 32;

    i_ret = stomp_others_parse(&p_data_in, &i_data_len, p_st_body_data, p_st_private);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_EQUAL(i_data_len, 0);

    frreport_body_free(&p_st_body_data);
    stomp_session_destroy(p_st_private);
    frfree(p_tmp);
    apmtest_destroy_ct(&p_st_conn);
    return;

}

static void __case2(void)
{
    L7session_stomp_private *p_st_private = NULL;
    char *p_data_in = NULL;
    int i_data_len = 0;
    int i_ret = 0;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = apmtest_create_ct(&p_st_conn, stomp_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 5672);
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    p_st_private = __stomp_session_private_create();

//    p_data_in = frmemdup("CONNECT\nlogin:admin\nhost:172.16.0.35\npasscode:password\naccept-version:1.1\n\0\n", 76);
//    p_tmp = p_data_in;
    i_data_len = 0;

    i_ret = stomp_others_parse(&p_data_in, &i_data_len, p_st_body_data, p_st_private);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    frreport_body_free(&p_st_body_data);
    stomp_session_destroy(p_st_private);
    apmtest_destroy_ct(&p_st_conn);
//    frfree(p_tmp);
    return;

}

static void __case3(void)
{
    L7session_stomp_private *p_st_private = NULL;
    char *p_data_in = NULL;
    int i_data_len = 0;
    int i_ret = 0;
    char *p_tmp = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = apmtest_create_ct(&p_st_conn, stomp_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 5672);
    p_st_body_data = frreport_body_new_l7 (p_st_conn);

    p_st_private = __stomp_session_private_create();

    p_data_in = (char *)frmemdup((char *)"ACK\nreceipt:3\nsubscription:1\n\n\0", 31);
    p_tmp = p_data_in;
    i_data_len = 31;

    i_ret = stomp_others_parse(&p_data_in, &i_data_len, p_st_body_data, p_st_private);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);

    frreport_body_free(&p_st_body_data);
    stomp_session_destroy(p_st_private);
    apmtest_destroy_ct(&p_st_conn);
    frfree(p_tmp);
    return;

}


int add_suite_stomp_others_parse(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" stomp_others_parse()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);


    return FR_SUCC;

}